package com.gsit.omega.ea.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gsit.omega.ea.domain.User;

@Component(value="currentUserService") 
public class CurrentUserServiceImpl implements CurrentUserService {

	
	@Autowired
	private UserService userService;
	
	
	public User getCurrentUser(){
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		if (principal instanceof org.springframework.security.core.userdetails.User) {
			org.springframework.security.core.userdetails.User principalUser = (org.springframework.security.core.userdetails.User) principal;
			System.out.println("The user detail is "+ principalUser.getUsername());

			// userService.findUser(id)
			List<User> list = userService.findAllUsers();
			for (User user : list) {
				if (user.getEmailAddress().equalsIgnoreCase(
						principalUser.getUsername())) {
					return user;
				}
			}

		}

		return null;
	}
	
}
